package com.zhangtai.modules.service.impl.performance;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.PerformanceUnitManagementDao;
import com.zhangtai.modules.entity.performance.PerformanceUnitManagementEntity;
import com.zhangtai.modules.service.performance.PerformanceUnitManagementService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class PerformanceUnitManagementServiceImpl extends ServiceImpl<PerformanceUnitManagementDao, PerformanceUnitManagementEntity> implements PerformanceUnitManagementService {

    @Override
    public PageResult queryPage( @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                 @RequestParam(value = "assessmentYear", required = false) String assessmentYear,
                                @RequestParam(value = "name",  required = false) String name,
                                @RequestParam(value = "assessmentCycle",  required = false) Integer assessmentCycle,
                                @RequestParam(value = "performanceName",  required = false) String performanceName,
                                 @RequestParam(value = "approveStatus", required = false) List<Integer> approveStatus,
                                 @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<PerformanceUnitManagementEntity> page = this.page(
                new Page<PerformanceUnitManagementEntity>(pageNum,pageSize),
                new QueryWrapper<PerformanceUnitManagementEntity>()
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .like(!StringUtils.isEmpty(performanceName),"performance_name",performanceName)
                        .like(!StringUtils.isEmpty(assessmentYear),"assessment_year",assessmentYear)
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .eq(assessmentCycle!=null,"assessment_cycle",assessmentCycle)
                        .in(!CollectionUtils.isEmpty(approveStatus),"approve_status",approveStatus)
                        .eq(status!=null,"status",status)
                        .eq("is_delete",0)
                        .orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public PerformanceUnitManagementEntity getByIdE(Long id) {
            PerformanceUnitManagementEntity one = this.getOne(new QueryWrapper<PerformanceUnitManagementEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(PerformanceUnitManagementEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(PerformanceUnitManagementEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            PerformanceUnitManagementEntity entity = new PerformanceUnitManagementEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}